package com.cy.pj.sys.service.impl;

import com.cy.common.annotation.RequiredLog;
import com.cy.common.annotation.RequiredTime;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.pojo.SysLog;
import com.cy.pj.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = false,timeout = 60,
        isolation = Isolation.READ_COMMITTED,//禁止出现脏读,保证性能,拒绝加锁 //事务并发执行,脏读,不可重复读,幻读,
        rollbackFor = Throwable.class)//当时间超过60可以设置 抛出异常执行回滚
@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired //通过构造函数为属性注入值
    private SysLogDao sysLogDao;
//     SysLogServiceImpl(SysLogDao sysLogDao){
//        this.sysLogDao=sysLogDao;
//    }

    //由此注解描述的方法为日志切入点方法
    @Transactional(readOnly = true)
    @RequiredLog(operation = "查询日志")
    @Override
    @RequiredTime
    public List<SysLog> selectLogs(SysLog sysLog) {
//        System.out.println("save.log.thread->"+Thread.currentThread().getName());
            List<SysLog> list = sysLogDao.selectLogs(sysLog);
            return list;
//            throw new RuntimeException("新增失败");
    }

    @Override
    public SysLog selectById(Long id) {
           SysLog log = sysLogDao.selectById(id);
           return log;
    }
    @Async  //描述的方法为一个异步切入点方法;底层会在一个新的线程里调用执行此方法;
    @Override
    public int insertLog(SysLog sysLog) {
//        System.out.println("select.log.thread->"+Thread.currentThread().getName());
//        try {
//            Thread.sleep(10000);
//        }catch(Exception e){}

        //查询和新增 日志共享一个线程
        int row = sysLogDao.insertLog(sysLog);
        return row;
    }
    @Transactional  //可视为事务切入点方法,可用在类和属性上
    @RequiredLog(operation = "删除日志")
    @Override
    public int deleteLog(Long... ids) {

        int rows = sysLogDao.deleteLog(ids);
        return rows;
    }
}
//作业springboot工具中AOP事务控制,@Transactional注解的应用
//即 多个删除操作同时访问,删除失败能否进行事务回滚